/**
 * Project Name:emm-website
 * File Name:CustomerGiveUpController.java
 * Package Name:com.emm.yixun.website.controller.customer
 * Date:2017年11月30日上午9:26:42
 * Copyright (c) 2017, shihao.liu@qiaofang.info All Rights Reserved.
 *
 */

package com.emm.yixun.website.controller.customer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emm.yixun.common.model.enums.MerchantEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.common.model.Customer;
import com.emm.yixun.common.model.Project;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.CustomerComponentDto;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.customer.service.CustomerServiceFacade;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
import com.emm.yixun.website.service.customer.SubscribeService;
import com.emm.yixun.website.utils.ExcelUtil;

/**
 * ClassName:CustomerGiveUpController <br/>
 * Function: 客户放弃. <br/>
 * Date: 2017年11月30日 上午9:26:42 <br/>
 * 
 * @author Administrator
 * @version
 * @since JDK 1.6
 * @see
 */
@RequestMapping(value = "/customer/giveUp")
@Controller
public class CustomerGiveUpController extends BaseController<Customer> {

	@Autowired
	private CustomerServiceFacade customerServiceFacade;
	
	@Autowired
	private SubscribeService subscribeService;

	@Autowired
	private ProjectServiceFacade projectServiceFacade;
	/**
	 * 
	 * giveUpCustomer:(客户放弃). <br/>
	 * 
	 * @author Administrator
	 * @param customerId
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "giveUpCustomer")
	public @ResponseBody AjaxRes giveUpCustomer(Long customerId) {
		AjaxRes ar = getAjaxRes();
		try {
			Response<Boolean> response = customerServiceFacade.giveUpCustomer(ShiroUtil.getCurrentUser(), customerId);
			if (!response.getEntity()) {
				ar.setRes(Const.FAIL);
				ar.setResMsg(response.getResultMessage());
			} else {
				ar.setRes(Const.SUCCEED);
			}
		} catch (Exception e) {
			logger.error("error", e);
		}
		return ar;
	}
	/**
	 * 
	 * list:(客户池). <br/>
	 * @author Administrator
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "list")
	public ModelAndView list() {
		try {
			if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
				
				return new ModelAndView("customer/giveUp/list")
						.addObject("permitBtn", doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "customer:list:all"));
			}
		} catch (Exception e) {
			logger.error("已放弃客户错误,{}", e);
		}
		return new ModelAndView(Const.NO_AUTHORIZED_URL);
	}
	/**
	 * 
	 * giveUpList:(已放弃客户池). <br/>
	 * @author Administrator
	 * @param dto
	 * @param page
	 * @param limit
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "giveUpList")
	public @ResponseBody Page<CustomerComponentDto> giveUpList(CustomerComponentDto dto, Integer page, Integer limit){
		Page<CustomerComponentDto> response = new Page<CustomerComponentDto>();
		try {
			dto = setQueryValue(dto);
			response = customerServiceFacade.findGiveUpPage(dto,PageUtil.begin(page, limit), limit);
			response.setPageNumber(page);
		} catch (Exception e) {
			logger.error("列表错误,{}", e);
		}
		return response;
	}
	/**
	 * 
	 * setQueryValue:(设置查询条件). <br/>
	 * @author Administrator
	 * @param dto
	 * @return
	 * @since JDK 1.6
	 */
	public CustomerComponentDto setQueryValue(CustomerComponentDto dto){
		User user = ShiroUtil.getCurrentUser();
		dto.setPermissionUserId(user.getId());
		dto.setMerchantId(user.getMerchantId());
		dto.setOrders(" t.resign_time desc ");
		if(!StringUtils.isEmpty(dto.getCreateTimeAfter())){
			dto.setCreateTimeAfter(dto.getCreateTimeAfter()+" 23:59:59");
		}
		if(!StringUtils.isEmpty(dto.getResignTimeAfter())){
			dto.setResignTimeAfter(dto.getResignTimeAfter()+ " 23:59:59");
		}
		if(StringUtils.isNotBlank(dto.getFtContactWay())){
			if(!MerchantEnum.GREEN_TOWN.getNo().equals(String.valueOf(user.getMerchantId()))){
				dto.setFtContactWayType("1");
			}
		}
		return dto;
	}
	/**
	 * 
	 * exportCustomerGiveUpList:(已放弃客户池导出). <br/>
	 * @author Administrator
	 * @param params
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "exportCustomerGiveUpList")
	public void exportCustomerGiveUpList(String params,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		try {
			CustomerComponentDto dto = JSONObject.parseObject(params, CustomerComponentDto.class);
			dto.setExport(true);
			dto = setQueryValue(dto);
			Page<CustomerComponentDto> response  = customerServiceFacade.findGiveUpPage(dto,0,1);
			if(response.getRows()!=null){
				List<CustomerComponentDto> custometList = response.getRows();
				String[] name = new String[]{"已放弃客户"}; //sheet信息
				List<String[]> headerList = new ArrayList<String[]>(); 
				List<List<Map<String, String>>> list = new ArrayList<List<Map<String, String>>>(); 
				String[] header = new String[]{"客户姓名","联系电话","客户性别","所属项目","意向等级","原经纪人","首次接触方式",
						"创建时间","最后跟进时间","放弃时间"};
				headerList.add(header);
				List<Map<String, String>> listMap = subscribeService.giveUpCustomerExport(custometList, header);
				list.add(listMap);
				ExcelUtil.exportExcel(list, headerList, name, httpServletResponse);
			}
		} catch (Exception e) {
			logger.error("export error",e);
		}
	}
	/**
	 * 
	 * projectList:(项目列表). <br/>
	 * @author Administrator
	 * @param dto
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "projectList")
	public @ResponseBody List<Project> projectList(ProjectDto dto){
		User user = ShiroUtil.getCurrentUser();
		dto.setPermissionUserId(user.getId());
		dto.setMerchantId(user.getMerchantId());
		ResponseList<Project> responseList = projectServiceFacade.findByDto(dto);
		return responseList.getEntity();
	}
	
	/**
	 * 
	 * getGiveUpCustomer:(认领已放弃的客户). <br/>
	 * @author Administrator
	 * @param customerId
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "getGiveUpCustomer")
	public @ResponseBody AjaxRes getGiveUpCustomer(Long customerId){
		AjaxRes ar = getAjaxRes();
		Response<Boolean> response = customerServiceFacade.getGiveUpCustomer(customerId, ShiroUtil.getCurrentUser());
		if(!response.getEntity()){
			ar.setRes(Const.FAIL);
			ar.setResMsg(response.getResultMessage());
		}else{
			ar.setRes(Const.SUCCEED);
		}
		return ar;
	}
	
}
